/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
/*
 * Created on Mar 5, 2005
 *
 */
package edu.mit.csail.relo.jdt.browse;

import org.apache.log4j.Logger;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.ui.parts.AbstractEditPartViewer;
import org.eclipse.jdt.core.IJavaElement;
import org.openrdf.model.Resource;

import edu.mit.csail.relo.agent.BrowseModel;
import edu.mit.csail.relo.jdt.CodeUnit;
import edu.mit.csail.relo.jdt.ReloJDTPlugin;
import edu.mit.csail.relo.modelBridge.Artifact;
import edu.mit.csail.relo.modelBridge.Comment;
import edu.mit.csail.relo.parts.ReloPartFactory;
import edu.mit.csail.relo.store.ReloRdfRepository;

/**
 * @author vineet
 *
 */
public abstract class AbstractJDTBrowseModel extends BrowseModel {
	static final Logger logger = ReloJDTPlugin.getLogger(AbstractJDTBrowseModel.class);
    
    @Override
    protected EditPartFactory createEditPartFactory(AbstractEditPartViewer viewer) {
        return new ReloPartFactory(this);
    }
    
    @Override
    public String getBundleSymbolicName() {
        return ReloJDTPlugin.getDefault().getBundle().getSymbolicName();
    }

	@Override
    public Artifact getArtifact(Object src) {
	    if (src instanceof IJavaElement)
	        return CodeUnit.getCodeUnit(getRepo(), (IJavaElement) src);
	    return super.getArtifact(src);
	}
    
    // @tag unify-core: Browse Models are only what should differentiate Relo,
    // Strata, etc, so we should not be reusing like this

    public static boolean isLoadableJavaArtifact(Artifact art, Resource artType, ReloRdfRepository repo) {
        if (artType == null)
            throw new IllegalArgumentException("Cached type needed for loadability");

        // we are right now removing project boundaries, for the traditional views
        if (CodeUnit.isJavaProject(repo, artType)) return false;

        if (!CodeUnit.isJavaElementType(repo, artType)) return false;
            
        // show only items that have the source available (and all packages)
        if (CodeUnit.isPackage(repo, artType))
            return true;

        // XXX removed check for source to view in Relo ==> we need to implement a strategy for this
        // TODO: why did we even need to comment the below out, this should be provided everywhere
        //if ( repo.hasStatement(art.elementRes, RJCore.srcResource, null))
        //    return true;

        if ( repo.hasStatement(art.elementRes, repo.rdfType, null))
            return true;

        return false;
    }

    @Override
    public boolean artifactLoadable(Artifact art, Resource artType) {
        if (isLoadableJavaArtifact(art, artType, getRepo()) || art instanceof Comment)
                return true;
        else
            return false;
	}

    @Override
    public void init(AbstractEditPartViewer viewer) {
        super.init(viewer);

        /*
        // TODO: hook up the handler to call into OpenTypeAction
        ReloEditor editorPart = (ReloEditor) ((DefaultEditDomain)viewer.getEditDomain()).getEditorPart();
        Action act = new Action("blah\tCtrl+Shift+T") {
            @Override
            public void run() {
                System.err.println("running");
            }
        };
        
        IMenuManager imm = ((ReloEditorContributor)editorPart.getEditorSite().getActionBarContributor()).getActionBars().getMenuManager();
        IMenuManager imm2 = (IMenuManager) imm.find("Relo");
        imm2.insertAfter("model", act);
        editorPart.getEditorSite().getActionBars().setGlobalActionHandler("org.eclipse.jdt.ui.navigate.open.type", act);
        editorPart.getEditorSite().getActionBars().setGlobalActionHandler("org.eclipse.jdt.ui.navigate.openType", act);
        */
    }
}
